/******************************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd. 		              */
/*																              */
/*	NAME	  = t__sqlserver_bigint_add.c						              */
/*	NOTE	  = 												              */
/*	DATE	  = 2014/11/25 by geyh								              */
/******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "apmtest_cunit.h"
#include "../test_db2_comm.h"
#include <db2/db2_core.h>
#include <db2/db2_comm.h>
#include <db2/db2_param.h>
#include <db2/db2_obj.h>
#include <apmtest_comm.h>
#include <db2/config.h>

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    int i_ret = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);

    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x00\x0b\xd0\x03\x00\x01\x00\x05\x24\x08\xff", 11, TEST_DB2_SRC_IP,
                                      TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT, st_timestamp), FR_SUCC);

    i_ret = db2_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    apmtest_destroy_apm_packet(&p_st_packet);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case2(void)
{
    int i_ret = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);

    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x00\x0b\xd0\x03\x00\x01\x00\x05\x11\x11\xff", 11, TEST_DB2_SRC_IP,
                                      TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT, st_timestamp), FR_SUCC);

    i_ret = db2_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case3(void)
{
    int i_ret = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);

    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x00\x09\xd0\x03\x00\x01\x00\x05\x24\x08\xff", 11, TEST_DB2_SRC_IP,
                                      TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT, st_timestamp), FR_SUCC);

    i_ret = db2_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case4(void)
{
    int i_ret = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);

    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x00\x0b\xd0\x03\x00\x01\x00\x04\x24\x08\xff", 11, TEST_DB2_SRC_IP,
                                      TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT, st_timestamp), FR_SUCC);

    i_ret = db2_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case5(void)
{
    int i_ret = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);

    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x00\x0b\xd0\x03\x00\x01\x00\x03\x24\x08\xff", 11, TEST_DB2_SRC_IP,
                                      TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT, st_timestamp), FR_SUCC);

    i_ret = db2_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case6(void)
{
    int i_ret = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);

    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x00\x0a\xd0\x03\x00\x01\x00\x04\x24\x08\xff", 11, TEST_DB2_SRC_IP,
                                      TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT, st_timestamp), FR_SUCC);

    i_ret = db2_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    apmtest_destroy_apm_packet(&p_st_packet);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

static void __case7(void)
{
    int i_ret = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(
            test_db2_create_ct(&p_st_conn, TEST_DB2_SRC_IP, TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT),
            FR_SUCC);

    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(
            apmtest_create_apm_packet(&p_st_packet, (char *) "\x00\x0b\xd1\x03\x00\x01\x00\x05\x24\x08\xff", 11, TEST_DB2_SRC_IP,
                                      TEST_DB2_DST_IP, TEST_DB2_SRC_PORT, TEST_DB2_DST_PORT, st_timestamp), FR_SUCC);

    i_ret = db2_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    apmtest_destroy_apm_packet(&p_st_packet);

    test_db2_destroy_ct(&p_st_conn);

    return;
}

int add_suite_db2_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" db2_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL ||
        CU_add_test(pSuite, "case6", __case6) == NULL ||
        CU_add_test(pSuite, "case7", __case7) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
